package com.young4j.springdemo.controller;

import com.young4j.springdemo.excel.ExcelLogs;
import com.young4j.springdemo.excel.ExcelUtil;
import com.young4j.springdemo.excel.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author yf
 * @Date 2019/2/14 17:45
 **/
@RestController
@RequestMapping("/excel")
public class ExcelController {
    @RequestMapping("/exportXlsByBean")
    public void exportXlsByBean() throws IOException {
        //用排序的Map且Map的键应与ExcelCell注解的index对应
        Map<String,String> map = new LinkedHashMap<>();
        map.put("a","姓名");
        map.put("b","年龄");
        map.put("c","性别");
        map.put("d","出生日期");
        Collection<Object> dataset=new ArrayList<Object>();
        dataset.add(new Model("", "", "",null));
        dataset.add(new Model(null, null, null,null));
        dataset.add(new Model("王五", "34", "男",new Date()));
        File f=new File("test.xls");
        OutputStream out =new FileOutputStream(f);
        ExcelUtil.exportExcel(map, dataset, out);
        out.close();
    }
    @RequestMapping("/exportXlsByMap")
    public void exportXlsByMap() throws IOException {
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map =new LinkedHashMap<>();
        map.put("name", "");
        map.put("age", "");
        map.put("birthday","");
        map.put("sex","");
        Map<String,Object> map2 =new LinkedHashMap<String, Object>();
        map2.put("name", "测试是否是中文长度不能自动宽度.测试是否是中文长度不能自动宽度.");
        map2.put("age", null);
        map2.put("sex", null);
        map.put("birthday",null);
        Map<String,Object> map3 =new LinkedHashMap<String, Object>();
        map3.put("name", "张三");
        map3.put("age", 12);
        map3.put("sex", "男");
        map3.put("birthday",new Date());
        list.add(map);
        list.add(map2);
        list.add(map3);
        Map<String,String> map1 = new LinkedHashMap<>();
        map1.put("name","姓名");
        map1.put("age","年龄");
        map1.put("birthday","出生日期");
        map1.put("sex","性别");
        File f= new File("test31.xls");
        OutputStream out = new FileOutputStream(f);
        ExcelUtil.exportExcel(map1,list, out );
        out.close();
    }
    @RequestMapping("/importXls")
    public void importXls(MultipartFile file) throws FileNotFoundException {
        try {
            InputStream inputStream = file.getInputStream();
            ExcelLogs logs =new ExcelLogs();
            //Collection<Map> importExcel = ExcelUtil.importExcel(Map.class, inputStream, "yyyy/MM/dd HH:mm:ss", logs , 0);
            Collection<Model> importExcel = ExcelUtil.importExcel(Model.class, inputStream, "yyyy-MM-dd", logs, 0);
            List<Model> collect = importExcel.stream().collect(Collectors.toList());
            collect.forEach(n ->{
                System.out.println(n.toString());
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
